﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Services.Client;

namespace AntsCode.StorageClientExtensions
{
    public class ContinuationToken
    {
        public ContinuationToken(string nextPartitionKey, string nextRowKey)
        {
            this.NextPartitionKey = nextPartitionKey;
            this.NextRowKey = nextRowKey;
        }

        internal static ContinuationToken GetFromQueryResponse<T>(QueryOperationResponse<T> response)
        {
            string nextPartitionKey = null;
            string nextRowKey = null;

            response.Headers.TryGetValue("x-ms-continuation-NextPartitionKey", out nextPartitionKey);
            response.Headers.TryGetValue("x-ms-continuation-NextRowKey", out nextRowKey);

            if (nextPartitionKey != null && nextRowKey != null)
            {
                // Update the continuation token
                return new ContinuationToken(nextPartitionKey, nextRowKey);
            }
            else
            {
                return null;
            }
        }

        public string NextPartitionKey
        {
            get;
            private set;
        }

        public string NextRowKey
        {
            get;
            private set;
        }
    }
}
